package com.cts.tmall.app.controller;

import com.cts.tmall.biz.service.TimingSaleService;
import com.cts.tmall.dal.entity.TmallTimingSale;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/api/timingSale")
public class TimingSaleController {

    @Autowired
    private TimingSaleService timingSaleService;

    @GetMapping
    public Map<String, Object> queryTimingSale(
            @RequestParam(name = "createUserId", required = false) Long createUserId,
            @RequestParam(name = "prdNo", required = false) String prdNo,
            @RequestParam(name = "prdName", required = false) String prdName,
            @RequestParam(name = "saleStartTime", required = false) String saleStartTimeStr,
            @RequestParam(name = "saleEndTime", required = false) String saleEndTimeStr,
            @RequestParam(name = "pageNumb", required = false, defaultValue = "1") Long pageNumb,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) throws Exception {
        Date saleStartTime = StringUtils.isNoneBlank(saleStartTimeStr) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(saleStartTimeStr) : null;
        Date saleEndTime = StringUtils.isNoneBlank(saleEndTimeStr) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(saleEndTimeStr) : null;
        return timingSaleService.queryTimingSale(createUserId,prdNo, prdName, saleStartTime, saleEndTime, pageNumb, pageSize);
    }

    @PostMapping
    public TmallTimingSale addTimingSale(@RequestBody TmallTimingSale timingSale) {
        return timingSaleService.addTimingSale(timingSale);
    }

    @PutMapping
    public TmallTimingSale updTimingSale(@RequestBody TmallTimingSale timingSale) {
        return timingSaleService.updTimingSale(timingSale);
    }
}
